//
//  MyFavCoursesVc.m
//  Courses
//
//  Created by xxx on 15/1/24.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "MyFavCoursesVc.h"
#import "FavCourseList.h"
#import "PublicCell.h"

#import "PublicDetailsVc.h"

#import "NSDate+WTF.h"

@implementation MyFavCoursesVc
//-(id)initWithIndex:(int)index{
//    _index = index;
//    return self;
//}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"我的收藏";
    //提示网络状态
    [self showNetStatus];
    //
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[FavCourseList favCourseLists] mutableCopy];
        NSLog(@"我的收藏数据%d",[self.items count]);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    });

    //界面刷新
    UIRefreshControl* rc = [[UIRefreshControl alloc] init];
    [rc addTarget:self action:@selector(refreshAction) forControlEvents:UIControlEventValueChanged];
    rc.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    self.refreshControl = rc;
    self.refreshControl.tintColor = [UIColor colorWithRed:16/255.0 green:101/255.0 blue:228/255.0 alpha:1.0];
}

- (void)refreshAction{
    NSLog(@"开始刷新！");
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        self.items = [[FavCourseList favCourseLists] mutableCopy];
        NSLog(@"我的收藏数据%d",[self.items count]);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.refreshControl endRefreshing];
            [self.tableView reloadData];
        });
    });
}
//界面刷新结束



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.items.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"我的收藏数据列表数据%d",[self.items[section] favCourses].count);
    return [self.items[section] favCourses].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect frame = AutoRectMake(0, 0, DEFAULT_WIDTH, 30, WTFAutoLayoutTypeBar);
    UIView *viewHeader = [[UIView alloc] initWithFrame:frame];
    viewHeader.backgroundColor = [WTFColors defaultBarColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    [viewHeader addSubview:label];
    
    if (self.items.count > section)
    {
        label.text = [self.items[section] markDate];
    }
    
    return viewHeader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PublicCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[PublicCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    if (self.items.count > indexPath.section)
    {
        if ([self.items[indexPath.section] favCourses].count > indexPath.row)
        {
            cell.course = [self.items[indexPath.section] favCourses][indexPath.row];
        }
    }
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    PublicDetailsVc *vc = [[PublicDetailsVc alloc] init];
//    PublicCourse *course = nil;
//    if (self.items.count > indexPath.section)
//    {
//        if ([self.items[indexPath.section] favCourses].count > indexPath.row)
//        {
//            course = [self.items[indexPath.section] favCourses][indexPath.row];
//            vc.courseId = course.courseId;
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//    }
}

@end
